# -------------------------------------------------------------------
#       -*- coding: utf-8 -*-
#   @Project    :   spider
#   @File       :   23_bs4.py
#   @Author     :   WANGYU
#   @Time       :   2021-08-17 19:50:29
#   @Software   :   PyCharm
#   @Desc       :   粮油行情
# -------------------------------------------------------------------


# 安装 表示
# 1.拿到页面源代码
# 2.使用bs4进行解析，拿到数据

import requests
from bs4 import BeautifulSoup
import csv

num = 1
while num < 2:
    url = f'https://www.jinnong.cn/querycompanylist/%E7%B2%AE%E6%B2%B9----.htm?pageNo={num}'
    # header = {
    #     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
    #                   'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
    # }
    # resp = requests.get(url, headers=header)
    resp = requests.get(url)
    # print(resp.text)
    num += 1
    # 写数据文件
    f = open('菜价.csv', mode='w')
    csv_writer = csv.writer(f)

    # 解析数据
    # 1.把页面源代码交给beautifulsoap进行处理，生成bs对象
    page = BeautifulSoup(resp.text, 'html.parser')  # 指定html解析器
    # # 2.从bs对象中查找数据
    table = page.find('div', class_='list-company-informations'). \
        find_all('a', attrs={'class': 'list-company-content'})
    # print(table)

    # # 拿到所有数据行
    # lis = table.find_all('li')[0:]  # 做切片
    # print(lis)
    for a in table:  # 每一行数据
        print(a.text)
        # print('ov')

        # name = a[1].text  # .text表示拿到被标签标记到内容
        # name = a[0].text  # .text表示拿到被标签标记到内容
        # low = a[1].text  # .text表示拿到被标签标记到内容
        # avg = a[2].text  # .text表示拿到被标签标记到内容
        # high = a[3].text  # .text表示拿到被标签标记到内容
        # gui = a[4].text  # .text表示拿到被标签标记到内容
        # kind = a[5].text  # .text表示拿到被标签标记到内容
        # data = a[6].text  # .text表示拿到被标签标记到内容
        # data2 = a[7].text  # .text表示拿到被标签标记到内容
        # print(name, low, avg, high, gui, kind, data,data2)
        #     csv_writer.writerow([name, low, avg, high, gui, kind, data])
        csv_writer.writerow(a.text)
    f.close()
    print('over')

